﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace RAP.Framework.Libary.Utils
{
	/// <summary>
	/// NameValueCollection扩展
	/// </summary>
	public static partial class Extensions
	{
		#region NameValueCollection扩展

		/// <summary>
		/// 
		/// </summary>
		/// <param name="col"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static string ToString ( this NameValueCollection col, string key, string defaultValue = null )
		{
            string value = col[ key ];
			return string.IsNullOrEmpty ( value ) ? defaultValue : value;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="col"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static bool ToBoolean ( this NameValueCollection col, string key, bool defaultValue = false )
		{
            return col.ToString ( key ).ToBooleanValue ( defaultValue );
		}

		#region ToDictionary

		/// <summary>
		/// 
		/// </summary>
		/// <param name="col"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static IDictionary ToDictionary ( this NameValueCollection col, string key, IDictionary defaultValue = null )
		{
			return col.ToString ( key ).ToDictionary ( );
        }

		#endregion ToDictionary

		#region ToList

		/// <summary>
		/// 
		/// </summary>
		/// <param name="col"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue">默认值</param>
		/// <returns></returns>
		public static IList ToList ( this NameValueCollection col, string key, IList defaultValue = null )
		{
			return col.ToString ( key ).ToListValue ( defaultValue );
        }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static IEnumerable<string> Values ( this NameValueCollection obj )
		{
			List<string> values = new List<string> ( );

			for ( int i = 0; i < obj.Count; i++ )
			{
				values.Add ( obj[ i ] );
			}
			return values;
		}
		#endregion ToList

		#region ToInt32

		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Int32 ToInt32 ( this NameValueCollection obj, string key, Int32 defaultValue = 0 )
		{
			return obj[ key ].ToInt32Value ( defaultValue );
		}
		
		#endregion ToInt32

		#region ToInt64

		/// <summary>
		/// 将指定键值的值对象转换成Int64类型，如果指定键不存在或值为空，则返回defaultValue
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Int64 ToInt64 ( this NameValueCollection obj, string key, Int64 defaultValue = 0 )
		{
			return obj[ key ].ToInt64Value ( defaultValue );
		}

		#endregion ToInt64

		#region ToDecimal

		/// <summary>
		/// 将指定键值的值对象转换成Decimal类型，如果指定键不存在或值为空，则返回defaultValue
		/// </summary>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Decimal ToDecimal ( this NameValueCollection obj, string key, Decimal defaultValue = 0.0m )
		{
			return obj[ key ].ToDecimalValue ( defaultValue );
		}

		#endregion ToDecimal

		#region ToSingle

		/// <summary>
		/// 将指定键值的值对象转换成Single类型，如果指定键不存在或值为空，则返回defaultValue
		/// </summary>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Single ToSingle ( this NameValueCollection obj, string key, float defaultValue = 0.0f )
		{
			return obj[ key ].ToFloatValue ( defaultValue );
		}

		#endregion ToSingle

		#region ToDouble

		/// <summary>
		/// 将指定键值的值对象转换成Double类型，如果指定键不存在或值为空，则返回defaultValue
		/// </summary>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Double ToDouble ( this NameValueCollection obj, string key, Double defaultValue = 0.0 )
		{
			return obj[ key ].ToDoubleValue ( defaultValue );
		}

		#endregion ToDouble

		#region ToInt16

		/// <summary>
		/// 将指定键值的值对象转换成Int16类型，如果指定键不存在或值为空，则返回defaultValue
		/// </summary>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static short ToInt16 ( this NameValueCollection obj, string key, short defaultValue = 0 )
		{
			return obj[ key ].ToShortValue ( defaultValue );
		}
		#endregion ToInt16

		#region ToDateTime

		/// <summary>
		/// 将指定键值的值对象转换成DateTime类型，如果指定键不存在或值为空，则返回defaultValue
		/// </summary>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static DateTime? ToDateTime ( this NameValueCollection obj, string key, DateTime? defaultValue = null )
		{
			object value;

			value = obj[ key ];
			if ( value == null )
			{
				return defaultValue;
			}
			try
			{
				return DateTime.Parse ( value.ToString ( ) );
			}
			catch
			{
			}
			return defaultValue;
		}

		/// <summary>
		/// 将指定键值的值按JSON协议转换成对象
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <returns></returns>
		public static IDictionary Eval ( this NameValueCollection obj, string key )
		{
			string value = obj.ToString ( key );
			if ( string.IsNullOrEmpty ( value ) )
			{
				return null;
			}
			return value.Eval ( ) as IDictionary;
		}

		#endregion ToDateTime

		#endregion NameValueCollection扩展
	}
}
